﻿
namespace UserDefinedForms.Windows.Forms
{
	/// <summary>
	/// abstract Supplier typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class SupplierDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<SupplierDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.Supplier _currentSupplier = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxSupplierDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxSupplierErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxSupplierBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the SupplierId property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxSupplierIdDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the SupplierName property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxSupplierNameDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Status property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxStatusDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.Supplier> _SupplierList;
				
		/// <summary> 
		/// The list of Supplier to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.Supplier> SupplierList
		{
			get {return this._SupplierList;}
			set
			{
				this._SupplierList = value;
				this.uxSupplierBindingSource.DataSource = null;
				this.uxSupplierBindingSource.DataSource = value;
				this.uxSupplierDataGridView.DataSource = null;
				this.uxSupplierDataGridView.DataSource = this.uxSupplierBindingSource;				
				//this.uxSupplierBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxSupplierBindingSource_ListChanged);
				this.uxSupplierBindingSource.CurrentItemChanged += new System.EventHandler(OnSupplierBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnSupplierBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentSupplier = uxSupplierBindingSource.Current as Entities.Supplier;
			
			if (_currentSupplier != null)
			{
				_currentSupplier.Validate();
			}
			//_Supplier.Validate();
			OnCurrentEntityChanged();
		}

		//void uxSupplierBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <see cref="Entities.Supplier"/> instance.
		/// </summary>
		public Entities.Supplier SelectedSupplier
		{
			get {return this._currentSupplier;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxSupplierDataGridView.VirtualMode;}
			set
			{
				this.uxSupplierDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxSupplierDataGridView.AllowUserToAddRows;}
			set {this.uxSupplierDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxSupplierDataGridView.AllowUserToDeleteRows;}
			set {this.uxSupplierDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <see cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxSupplierDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxSupplierDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="SupplierDataGridViewBase"/> class.
		/// </summary>
		public SupplierDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxSupplierDataGridView = new System.Windows.Forms.DataGridView();
			this.uxSupplierBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxSupplierErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxSupplierIdDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxSupplierNameDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxStatusDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			((System.ComponentModel.ISupportInitialize)(this.uxSupplierDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxSupplierBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxSupplierErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxSupplierErrorProvider
			// 
			this.uxSupplierErrorProvider.ContainerControl = this;
			this.uxSupplierErrorProvider.DataSource = this.uxSupplierBindingSource;						
			// 
			// uxSupplierDataGridView
			// 
			this.uxSupplierDataGridView.AutoGenerateColumns = false;
			this.uxSupplierDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxSupplierDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxSupplierIdDataGridViewColumn,
		this.uxSupplierNameDataGridViewColumn,
		this.uxStatusDataGridViewColumn			});
			this.uxSupplierDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxSupplierDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxSupplierDataGridView.Name = "uxSupplierDataGridView";
			this.uxSupplierDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxSupplierDataGridView.TabIndex = 0;	
			this.uxSupplierDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxSupplierDataGridView.EnableHeadersVisualStyles = false;
			this.uxSupplierDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnSupplierDataGridViewDataError);
			this.uxSupplierDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnSupplierDataGridViewCellValueNeeded);
			this.uxSupplierDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnSupplierDataGridViewCellValuePushed);
			
			//
			// uxSupplierIdDataGridViewColumn
			//
			this.uxSupplierIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxSupplierIdDataGridViewColumn.DataPropertyName = "SupplierId";
			this.uxSupplierIdDataGridViewColumn.HeaderText = "SupplierId";
			this.uxSupplierIdDataGridViewColumn.Name = "uxSupplierIdDataGridViewColumn";
			this.uxSupplierIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxSupplierIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxSupplierIdDataGridViewColumn.ReadOnly = true;		
			//
			// uxSupplierNameDataGridViewColumn
			//
			this.uxSupplierNameDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxSupplierNameDataGridViewColumn.DataPropertyName = "SupplierName";
			this.uxSupplierNameDataGridViewColumn.HeaderText = "SupplierName";
			this.uxSupplierNameDataGridViewColumn.Name = "uxSupplierNameDataGridViewColumn";
			this.uxSupplierNameDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxSupplierNameDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxSupplierNameDataGridViewColumn.ReadOnly = false;		
			//
			// uxStatusDataGridViewColumn
			//
			this.uxStatusDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxStatusDataGridViewColumn.DataPropertyName = "Status";
			this.uxStatusDataGridViewColumn.HeaderText = "Status";
			this.uxStatusDataGridViewColumn.Name = "uxStatusDataGridViewColumn";
			this.uxStatusDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxStatusDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxStatusDataGridViewColumn.ReadOnly = false;		
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxSupplierDataGridView);
			this.Name = "SupplierDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			((System.ComponentModel.ISupportInitialize)(this.uxSupplierErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxSupplierDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxSupplierBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				SupplierDataGridViewEventArgs args = new SupplierDataGridViewEventArgs();
				args.Supplier = _currentSupplier;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class SupplierDataGridViewEventArgs : System.EventArgs
		{
			private Entities.Supplier	_Supplier;
	
			/// <summary>
			/// the  Entities.Supplier instance.
			/// </summary>
			public Entities.Supplier Supplier
			{
				get { return _Supplier; }
				set { _Supplier = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxSupplierDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnSupplierDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxSupplierDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnSupplierDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxSupplierDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxSupplierIdDataGridViewColumn":
						e.Value = SupplierList[e.RowIndex].SupplierId;
						break;
					case "uxSupplierNameDataGridViewColumn":
						e.Value = SupplierList[e.RowIndex].SupplierName;
						break;
					case "uxStatusDataGridViewColumn":
						e.Value = SupplierList[e.RowIndex].Status;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxSupplierDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnSupplierDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxSupplierDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxSupplierIdDataGridViewColumn":
						SupplierList[e.RowIndex].SupplierId = (System.Int32)e.Value;
						break;
					case "uxSupplierNameDataGridViewColumn":
						SupplierList[e.RowIndex].SupplierName = (System.String)e.Value;
						break;
					case "uxStatusDataGridViewColumn":
						SupplierList[e.RowIndex].Status = (System.Int32)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
